﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InfoPick.Lib.Entity;
using InfoPick.Lib.Tools;

namespace InfoPick.Lib.StringResolve
{
    public class MTP2LNKResolver
    {
        public static List<MTP2LNK> ResolveDataWithKey(string str)
        {
            List<MTP2LNK> list = new List<MTP2LNK>();
            if (!string.IsNullOrEmpty(str))
            {
                const string startIdentifier = "查询 MTP2LNK\r\n------------";
                const string endIdentifier = "(结果个数";
                const string split = "\r\n\r\n";
                const string startWith = "链路号 ";

                var formList = StringTools.StrFilter(str, startIdentifier, endIdentifier, 0, new string[] { split }, startWith);

                foreach (var item in formList)
                {
                    try
                    {
                        var table = StringTools.StrFilter(item, "", "", 0, new string[] { "\r\n" });

                        var dict = StringTools.StrTable2Dict(table, new string[] { "=" });

                        MTP2LNK obj = new MTP2LNK();
                        obj.MTP2LinkNo = int.Parse(dict["链路号"]);
                        obj.BoardTypeName = dict["接口板类型"];
                        obj.BoradNum = int.Parse(dict["接口板板组号"]);
                        int tmp;
                        if(int.TryParse(dict["光口号"],out tmp))
                        {
                            obj.LightNum = tmp;
                        }
                        obj.E1T1 = int.Parse(dict["E1T1号"]);
                        obj.StartNum = int.Parse(dict["开始时隙号"]);
                        obj.EndNum = int.Parse(dict["结束时隙号"]);
                        obj.SPFBoardNum = int.Parse(dict["SPF板板组号"]);
                        obj.KNum = int.Parse(dict["扣板号"]);
                        list.Add(obj);
                    }
                    catch (Exception ex)
                    {
                        string message = string.Format("{0} : message:{1}\r\n{2}", DateTime.Now, ex.Message, ex.StackTrace);
                        Log.Log.WriteLog(message);
                    }
                }

            }
            return list;
        }

        public static List<MTP2LNK> ResolveData(string str)
        {
            List<MTP2LNK> list = new List<MTP2LNK>();
            if (!string.IsNullOrEmpty(str))
            {
                var table = StringTools.GetTable(str, "查询 MTP2LNK", "(结果个数");
                foreach (var item in table)
                {
                    var obj = MTP2LNK.Create(item);
                    if (obj != null)
                        list.Add(obj);
                }
            }
            return list;
        }
    }
}
